package at.horario.web.property.editor;

import org.apache.wicket.model.LoadableDetachableModel;

import java.io.Serializable;

import at.horario.service.GenericManager;
import at.horario.model.GenericObject;


/**
 * A detachable model that can load an Horario object from persistent store.
 *
 * @author ivaynberg
 */
public class GenericModel<T extends GenericObject, PK extends Serializable> extends LoadableDetachableModel {
    private GenericManager<T, PK> genericManager;
    private PK id;

    public GenericModel(GenericManager<T, PK> genericManager) {
        this.genericManager = genericManager;
    }

    /**
     * @param baseObject object this model will represent
     */
    public GenericModel(GenericObject<PK> baseObject, GenericManager<T, PK> genericManager) {
        super(baseObject);
        this.id = baseObject.getId();
        this.genericManager = genericManager;
    }

    protected T load() {
        return genericManager.get(id);
    }

}